/*
 * Copyright (c) 2023, 2024,yzc.cn All rights reserved.
 *
 */
package cn.yzc.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * <p>Project: SpringColud - TokenInterceptor</p>
 * <p>Powered by yzc On 2024-01-02 14:38:19</p>
 * <p>描述：<p>
 *
 * @author yzc [yzc0415@126.com]
 * @version 1.0
 * @since 17
 */
public class TokenInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("x-token");
        if(null == token){
            respJson(response,"没有token，无权访问");
            return false;
        }
        return true;
    }

    void respJson(HttpServletResponse response,String msg) throws IOException {
        response.setContentType("application/json;charset=utf-8");
        PrintWriter pw = response.getWriter();
        pw.write("{\"code\":0,\"msg\":\""+msg+"\"}");
    }
}
